package com.kingyea.mobilepolice.apimanage.accesscontrol.service.impl;

import com.kingyea.mobilepolice.apimanage.accesscontrol.bean.CustomController;
import com.kingyea.mobilepolice.apimanage.accesscontrol.dao.CustomControllerMapper;
import com.kingyea.mobilepolice.apimanage.accesscontrol.service.CustomControllerService;
import com.kingyea.mobilepolice.apimanage.association.dao.ServiceRequesterAssociationMapper;
import com.kingyea.mobilepolice.apimanage.systemsync.Send2Queue;
import com.kingyea.mobilepolice.apimanage.utils.page.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenjw
 * @version v1.0.0
 * @description CustomController 实现类
 * @date Created in 2018-04-03 15:32:08
 */
@Service
@Transactional
public class CustomControllerServiceImpl implements CustomControllerService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomControllerServiceImpl.class);

    @Resource
    private CustomControllerMapper customControllerMapper;
    @Resource
    private ServiceRequesterAssociationMapper serviceRequesterAssociationMapper;

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @Override
    public CustomController getById(Integer id) throws Exception {
        return this.customControllerMapper.getById(id);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public List<CustomController> list(CustomController customController) throws Exception {
        List<CustomController> list = customControllerMapper.list(customController);
        return list;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Send2Queue
    @Override
    public int deleteById(Integer id) throws Exception {
        customControllerMapper.deleteById(id);
        return 1;
    }

    /**
     * 保存
     *
     * @param customController
     * @return
     */
    @Send2Queue
    @Override
    public CustomController save(CustomController customController) throws Exception {
        try {
            customControllerMapper.save(customController);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customController;
    }

    /**
     * 更新
     *
     * @param customController
     * @return
     */
    @Send2Queue
    @Override
    public CustomController update(CustomController customController) throws Exception {
        try {
            customControllerMapper.update(customController);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customController;
    }

    @Override
    public List<CustomController> getByReqAndServiceId(Integer senderID, Integer serviceID) {
        return null;
    }

    @Override
    public Object searchByCondition(Map<String, Object> map) throws Exception {
        PageInfo.startPage(map);
        map.put("status", 3);
        List list = serviceRequesterAssociationMapper.searchByCondition(map);
        return PageInfo.getPageInfo(list);
    }

    @Override
    public CustomController getByParam(Map<String, Object> map) throws Exception {

        return customControllerMapper.getByParam(map);
    }

    @Send2Queue
    @Override
    public void addOrSaveACCInfo(CustomController cusController) throws Exception {
        //判断是否存在
        Map<String, Object> map = new HashMap<>();
        map.put("serviceId", cusController.getServiceId());
        map.put("requesterId", cusController.getRequesterId());
        CustomController tmp = customControllerMapper.getByParam(map);
        if (tmp == null) {
            customControllerMapper.save(cusController);
        } else {
            customControllerMapper.update(cusController);
        }
    }


}
